﻿using System;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LoanRescheduling : BasePage<LoanReschedulingViewModel>, IPermittedPage
    {

        /// <summary>
        /// Gets the loan ID.
        /// </summary>
        private string LoanID
        {
            get { return Request.QueryString["LoanID"].ToString(); }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                if (!LoanID.IsNullOrEmpty())
                {
                    ICustomerLoan loan = this.ViewModel.GetLoan(LoanID.ToGuid());
                    if (loan != null)
                    {
                        txtLoanNumber.Text = loan.LoanNumber.ToString();
                        txtCustomerName.Text = loan.CustomerObj.CustomerName;
                        txtBalanceToReschedule.Text = Math.Round(this.ViewModel.getLoanDues(LoanID.ToGuid()), 2, MidpointRounding.AwayFromZero).ToString();
                        dateFirstInstallmentDate.MinDate = DateTime.Today;
                    }
                }
                else
                {
                    this.RedirectToPreviousPage();
                }
            }
        }


        /// <summary>
        /// Handles the Clicked event of the BtnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnSubmit_Clicked(object sender, AjaxEventArgs e)
        {
            if (!LoanID.IsNullOrEmpty())
            {
                this.ViewModel.Reschedule(LoanID.ToGuid(), Convert.ToDateTime(dateFirstInstallmentDate.Value.ToString()), nufMonthCount.Value.ToInt32(),
                    insertAttchment.GetInsertedIDs(), insertAttchment.GetDeletedIDs());
                this.RedirectToPreviousPage();
            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnInstallmentTable control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnInstallmentTable_Clicked(object sender, AjaxEventArgs e)
        {
            if (!LoanID.IsNullOrEmpty())
            {
                //storeLoanInstallments.DataSource = this.ViewModel.GetExecutionInstallments(LoanID.ToGuid(), Convert.ToDateTime(dateFirstInstallmentDate.Value), nufMonthCount.Value.ToInt32());
                //storeLoanInstallments.DataBind();

                //wndLoanExecutionInstallments.Show();

                var installments = from inst in this.ViewModel.GetExecutionInstallments(LoanID.ToGuid(), Convert.ToDateTime(dateFirstInstallmentDate.Value), nufMonthCount.Value.ToInt32())
                                   select new
                                   {
                                       InstallmentDate = inst.InstallmentDate,
                                       OriginalInstallmentAmount = Math.Round(inst.OriginalInstallmentAmount, 2),
                                       InterestInstallmentAmount = Math.Round(inst.InterestInstallmentAmount, 2),
                                       TotalInstallmentAmount = Math.Round(inst.TotalInstallmentAmount)
                                   };
                Session.Add("ReschdulingInstallmentsList", installments);
                wndInstallmentsPrint.LoadContent("~/Loan.route/InstallmentsReschedulePrint");
                wndInstallmentsPrint.Show();
            }
        }


        /// <summary>
        /// Handles the Closed event of the wndInstallmentsPrint control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void wndInstallmentsPrint_Closed(object sender, AjaxEventArgs e)
        {
            if (Session["ReschdulingInstallmentsList"] != null)
            {
                Session.Remove("ReschdulingInstallmentsList");
            }
        }

        public string PermissionName
        {
            get { return Permissions.LoanRescheduling.ToString(); }
        }
    }
}
